package com.yjz.study.ui.activity

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.yjz.common.base.BaseActivity
import com.yjz.common.extend.showToast
import com.yjz.study.R
import com.yjz.study.adapter.account.CourseNoticeItem
import com.yjz.study.arouter.ARouterLogic

import com.yjz.study.http.entity.Resource.Status.*
import com.yjz.study.viewmodel.CourseViewModel
import com.yjz.ui.recycler.item.HiAdapter

/**
 *作用 :
 *作者 : yjz
 *时间 : 2025/5/23 15:17
 */
@Route(path = ARouterLogic.Path.NOTICE)
class NoticeActivity : BaseActivity(), View.OnClickListener {

    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(CourseViewModel::class.java)
    }

    override fun getLayoutId(): Int = R.layout.activity_notice

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        findViewById<View>(R.id.action_back).setOnClickListener(this)
        val recyclerView = findViewById<RecyclerView>(R.id.notice_recycler_view)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = HiAdapter(this@NoticeActivity)
        viewModel.requestCourseNoticeData().observe(this) { resource ->
            if (!TextUtils.isEmpty(resource.msg)) {
                showToast(resource.msg)
            }
            when (resource.status) {
                LOADING -> {

                }

                ERROR -> {

                }

                SUCCESS -> {
                    val noticeList = resource.data
                    val items = arrayListOf<CourseNoticeItem>()
                    noticeList?.forEach { item ->
                        items.add(CourseNoticeItem(item))
                    }
                    val adapter = recyclerView.adapter as HiAdapter
                    adapter.addItems(items, true)
                }
            }
        }
    }


    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.action_back -> {
                finish()
            }
        }
    }
}